package com.sinacom.ibuilding.action;




import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.sinacom.ibuilding.beans.Member;
import com.sinacom.ibuilding.beans.Role;
import com.sinacom.ibuilding.beans.Schedule;
import com.sinacom.ibuilding.service.ScheduleService;
import com.sinacom.ibuilding.util.Helper;
import com.sinacom.ibuilding.util.PageModel;

public class ScheduleAction {
	private ScheduleService scheduleService = new ScheduleService();
	private int page = 1;
	private PageModel scheduleList;
	private Schedule schedule;
	
	
	private String id;
	
	private String yearMonth;
	public String view()throws Exception{
	    HttpServletRequest request = ServletActionContext.getRequest(); 
	    String day = request.getParameter("day");
        Date date = null;
        if(!StringUtils.isEmpty(day)){
        	date = Helper.parseDate(day, "yyyy-MM-dd");
        }
	    scheduleList = scheduleService.getScheduleDay(date, page);
	    request.setAttribute("day", day);
		return "view";
	}
	
	public String schedulelist()throws Exception{
        HttpServletRequest request = ServletActionContext.getRequest(); 
        String day = request.getParameter("day");
        
        Date date = null;
        if(!StringUtils.isEmpty(day)){
        	date = Helper.parseDate(day, "yyyy-MM-dd");
        }
        scheduleList = scheduleService.getScheduleDay(date, page);
        request.setAttribute("day", day);
        return "schedulelist"; 
    }
	
	public String manage()throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		Member member = (Member) request.getSession().getAttribute("member");
		Role role = member.getRole();
		if(role.getScheduleMang() != null && role.getScheduleMang().booleanValue()){
			String day = request.getParameter("day");
			Date date = null;
			if(!StringUtils.isEmpty(day)){
				date = Helper.parseDate(day, "yyyy-MM-dd");
			}
		    scheduleList = scheduleService.getScheduleDay(date, page);
		    request.setAttribute("day", day);
		    return "manage";
		}else{
			request.setAttribute("msg", "您没有管理权限");
			return "error";
		}
	    
		
	}
	
	public String add()throws Exception{
        
        HttpServletRequest request = ServletActionContext.getRequest(); 
        Member member = (Member) request.getSession().getAttribute("member");
        Role role = member.getRole();
        if(role.getScheduleMang() != null && role.getScheduleMang().booleanValue()){
            String day = request.getParameter("day");
            if(!StringUtils.isEmpty(day)){
                scheduleList = scheduleService.getScheduleDay(Helper.parseDate(day, "yyyy-MM-dd"), page);
                if(scheduleList.getList() != null && scheduleList.getList().size() > 0){
                    HttpServletResponse response = ServletActionContext.getResponse(); 
                    response.sendRedirect("schedule!manage.do?day="+day);
                }
            }
            request.setAttribute("day", day);
            return "add";
        }else{
            request.setAttribute("msg", "您没有管理权限");
            return "error";
        }
    }
	
	public String edit()throws Exception{
        
        HttpServletRequest request = ServletActionContext.getRequest(); 
        Member member = (Member) request.getSession().getAttribute("member");
        Role role = member.getRole();
        if(role.getScheduleMang() != null && role.getScheduleMang().booleanValue()){
            schedule = scheduleService.getSechuleById(id);
            return "edit";
        }else{
            request.setAttribute("msg", "您没有管理权限");
            return "error";
        }
    }
	
	public String delete()throws Exception{
        
	    HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = null;
        pw = response.getWriter();
        HttpServletRequest request = ServletActionContext.getRequest(); 
        Member member = (Member) request.getSession().getAttribute("member");
        Role role = member.getRole();
        if(role.getScheduleMang() != null && role.getScheduleMang().booleanValue()){
            boolean status = scheduleService.deleteSchedule(id);
            
            Map map = new HashMap();  
            if(status){
                map.put("success", "true");
                map.put("msg", "删除日程成功");
            }else{
                map.put("success", "false");
                map.put("msg", "删除日程失败");
            }
            pw.print(JSONObject.fromObject(map));
            return null;
        }else{
            Map map = new HashMap();  
            map.put("success", "false");
            map.put("msg", "您没有管理权限");
            pw.print(JSONObject.fromObject(map));
            return null;
        }
    }
	
	public String read()throws Exception{
        schedule = scheduleService.getSechuleById(id);
        return "read";
       
    }
	
	public String save()throws Exception{
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = null;
        pw = response.getWriter();
        HttpServletRequest request = ServletActionContext.getRequest(); 
        Member member = (Member) request.getSession().getAttribute("member");
        Role role = member.getRole();
        if(role.getScheduleMang() != null && role.getScheduleMang().booleanValue()){
            boolean status = scheduleService.saveSchedule(member, schedule);
            
            Map map = new HashMap();  
            if(status){
                map.put("success", "true");
                map.put("msg", "日程发布成功");
            }else{
                map.put("success", "false");
                map.put("msg", "日程发布失败");
            }
            pw.print(JSONObject.fromObject(map));
            return null;
        }else{
            Map map = new HashMap();  
            map.put("success", "false");
            map.put("msg", "您没有管理权限");
            pw.print(JSONObject.fromObject(map));
            return null;
        }
        
    }
	
	public String hasdays() throws IOException{
        HttpServletResponse response = ServletActionContext.getResponse(); 
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = null;
        pw = response.getWriter();
        Map map = new HashMap(); 
        try{
            List days = (List) scheduleService.hasSchedules(Helper.parseDate(yearMonth, "yyyy-MM")); 
            
            map.put("days", days);
            map.put("success", "true");
        }catch(Exception e){
        	e.printStackTrace();
            map.put("success", "false");
            map.put("msg", "得到月份失败");
        }
        
        pw.print(JSONObject.fromObject(map));
        return null;
    }
	
	
	public int getPage() {
        return page;
    }


    public void setPage(int page) {
        this.page = page;
    }


    public PageModel getScheduleList() {
        return scheduleList;
    }


    public void setScheduleList(PageModel scheduleList) {
        this.scheduleList = scheduleList;
    }


    public Schedule getSchedule() {
        return schedule;
    }


    public void setSchedule(Schedule schedule) {
        this.schedule = schedule;
    }


    public String getId() {
        return id;
    }


    public void setId(String id) {
        this.id = id;
    }
    

    public String getYearMonth() {
        return yearMonth;
    }

    public void setYearMonth(String yearMonth) {
        this.yearMonth = yearMonth;
    }

    public static void main(String[] args) {
		System.out.println(DigestUtils.md5Hex("111111"));
	}
	
	
}
